﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Transactions;
//using System.Transactions;


namespace Core
{
    public partial class ExpenseLine
    {

        #region CRUD

        //public event EventHandler Deleting;

        public static IEnumerable<ExpenseLine> LoadSearchResult(DAL.DataAccessLayer dal, Guid expenseReportId)
        {
            DataAccessDataContext db = dal.DataContext;
            return (from l in db.ExpenseLines
                    where l.ExpenseReportId == expenseReportId
                    select l).ToArray<ExpenseLine>();
        }

        public static ExpenseLine Get(DAL.DataAccessLayer dal, Guid id)
        {
            DataAccessDataContext db = dal.DataContext;

            var q = from e in db.ExpenseLines
                    where e.Id.Equals(id)
                    select e;
            int count = q.Count<ExpenseLine>();
            ExpenseLine el = null;
            if (q.Count<ExpenseLine>() == 1)
                el = q.First<ExpenseLine>();
            if (count == 1)
                return q.First<ExpenseLine>();
            else
                return null;
        }

        public void Save(DAL.DataAccessLayer dal)
        {
            DataAccessDataContext da = dal.DataContext;
            var q = from e in da.ExpenseLines
                    where e.Id.Equals(this.Id)
                    select e;
            int count = q.Count<ExpenseLine>();
            ExpenseLine el = null;
            if (count == 1)
                el = q.First<ExpenseLine>();
            if (el == null)
                da.ExpenseLines.InsertOnSubmit(this);
            else
            {
                //el = new ExpenseLine();
                el.Date = this.Date;
                el.Description = this.Description;
                el.ExpenseReportId = this.ExpenseReportId;
                el.ExpenseTypeId = this.ExpenseTypeId;
                el.Id = this.Id;
                el.Justification = this.Justification;
                el.Value = this.Value;
            }
            //da.ExpenseLines.InsertOnSubmit(this);
            da.SubmitChanges();
        }

        //public static void OnDelete(object sender, EventArgs ea)
        //{
        //    ExpenseLine e = null;
        //}

        //public void FireDeleteEvent()
        //{
        //    this.Deleting(this, null);
        //}

        public static void Delete(DAL.DataAccessLayer dal, Guid lineId)
        {
            DataAccessDataContext db = dal.DataContext;
            //ExpenseLine line = db.ExpenseLines.First(l => l.Id == lineId);
            ExpenseLine line = ExpenseLine.Get(dal, lineId);
            //Console.WriteLine(line.ExpenseLineSimple.Value);
            //ExpenseLineSimple els = line.ExpenseLineSimple;
            //ExpenseLineDislocation eld = line.ExpenseLineDislocation;
            //ExpenseLineSimple.Get(dal, lineId);
            //line.Deleting += OnDelete;
            //line.Deleting(line, null);

            if (line != null)
            {
                //if (line.ExpenseLineDislocation != null)
                //{
                //    //db.ExpenseLineDislocations.Attach(line.ExpenseLineDislocation);
                //    db.ExpenseLineDislocations.DeleteOnSubmit(line.ExpenseLineDislocation);
                //}
                //if (line.ExpenseLineSimple != null)
                //{
                //    //db.ExpenseLineSimples.Attach(line.ExpenseLineSimple);
                //    db.ExpenseLineSimples.DeleteOnSubmit(line.ExpenseLineSimple);
                //}
                //db.ExpenseLines.Attach(line);
                db.ExpenseLines.DeleteOnSubmit(line);
                db.SubmitChanges();
            }
        }

      




        //public void Delete()
        //{
        //    DataAccessDataContext db = new DataAccessDataContext();
        //    if (this.ExpenseLineDislocation != null)
        //    {
        //        db.ExpenseLineDislocations.Attach(this.ExpenseLineDislocation);
        //        db.ExpenseLineDislocations.DeleteOnSubmit(this.ExpenseLineDislocation);
        //    }
        //    if (this.ExpenseLineSimple != null)
        //    {
        //        db.ExpenseLineSimples.Attach(this.ExpenseLineSimple);
        //        db.ExpenseLineSimples.DeleteOnSubmit(this.ExpenseLineSimple);
        //    }
        //    db.ExpenseLines.Attach(this);
        //    db.ExpenseLines.DeleteOnSubmit(this);
        //    db.SubmitChanges();

        //}

        #endregion

        private static LinkedList<Guid> RetrieveAllDepartmentsAssociatedWithUser(string userid, DAL.DataAccessLayer dc)
        {
            LinkedList<Guid> deps = new LinkedList<Guid>();
            var query = from deplevel in dc.DataContext.UserDepartments
                        where deplevel.UserId.Equals(userid)
                        select deplevel;

            foreach (UserDepartment ud in query)
                deps.AddFirst(ud.Id);

            return deps;
        }



        

        public static ExpenseReport GetReport(DAL.DataAccessLayer dc, Guid expenseid)
        {            
            var q = from e in dc.DataContext.ExpenseLines
                    where e.Id.Equals(expenseid)
                    select e;

            ExpenseLine el = q.Single<ExpenseLine>();
            return el.ExpenseReport;            
        }   
    }
}
